package form;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.User;

import dao.UserDAO;

/**
 * Servlet implementation class Xulydoimatkhau
 */
@WebServlet("/Xulydoimatkhau")
public class Xulydoimatkhau extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String matkhaucu = "", matkhaumoi = "", re_matkhaumoi = "";
	private String err_changepass, finish_changepass;
	private boolean check;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Xulydoimatkhau() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		toDo(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		toDo(request, response);
	}

	private void toDo(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		matkhaucu = request.getParameter("matkhaucu");
		matkhaumoi = request.getParameter("matkhaumoi");
		re_matkhaumoi = request.getParameter("re_matkhaumoi");
		matkhaumoi = matkhaumoi.trim();
		re_matkhaumoi = re_matkhaumoi.trim();
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		finish_changepass = "";
		try {
			if (matkhaucu == "") {
				err_changepass = "Bạn chưa nhập mật khẩu cũ";

			} else {
				check = UserDAO.login(user.getEmail(), matkhaucu);
				if (!check) {
					err_changepass = "Mật khẩu cũ không đúng";
				} else {
					if (matkhaumoi.equals(re_matkhaumoi)
							&& !matkhaumoi.equalsIgnoreCase("")
							&& !re_matkhaumoi.equalsIgnoreCase("")) {

						user.setMatkhau(matkhaumoi);
						UserDAO.updatePassword(user);
						finish_changepass = "Thay đổi mật khẩu thành công";
						request.setAttribute("finish_changepass",
								finish_changepass);
					} else {
						err_changepass = "Mật khẩu xác nhận không trùng khớp";
					}
				}
			}
		} catch (SQLException | ClassNotFoundException e) {
			String err = "Hệ thống đã bị lỗi chúng tôi sẽ nhanh chóng khắc phục";
			request.setAttribute("err", err);
			response.sendRedirect("danhnhap.jsp");

		}
		request.setAttribute("err_changepass", err_changepass);
		RequestDispatcher rd = getServletContext().getRequestDispatcher(
				"/trangcanhan.jsp");
		rd.forward(request, response);
	}
}
